<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
        <el-form-item class="el-form-item-align-right">
          <el-button icon="el-icon-circle-plus" size="mini" type="primary" @click="openClassificationEdit()" v-has-permi="['admin:merchantActivities:buying:list']">团购分类列表</el-button>
          <el-button icon="el-icon-circle-plus" size="mini" type="primary" @click="openEdit()" v-has-permi="['admin:merchantActivities:save']">添加</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="listData.list" :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}"
                border size="mini">
        <el-table-column coverImg="120" label="ID" prop="id"></el-table-column>
        <el-table-column label="活动名称" min-width="130" prop="activityName"/>
        <el-table-column label="现价" min-width="80" prop="price"/>
        <el-table-column label="原价" min-width="80" prop="originalPrice"/>
        <el-table-column label="库存" min-width="80" prop="number"/>
        <el-table-column label="优惠折扣" min-width="60" prop="preferential"/>
        <el-table-column label="有效期限" min-width="120" prop="validPeriod"/>
        <el-table-column label="是否甄选活动" min-width="80" prop="pick"/>
        <el-table-column align="center" fixed="right" label="操作" min-width="180">
          <template slot-scope="scope">
            <el-button v-has-permi="['admin:merchantActivities:save']" icon="el-icon-edit" size="mini" type="primary"
                       @click="openEdit(scope.row.id)">编辑
            </el-button>
            <el-button v-has-permi="['admin:merchantActivities:delete']" icon="el-icon-delete" size="mini" type="danger"
                       @click="handlerDelete(scope.row.id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          :current-page="listPram.page"
          :page-sizes="[20,40,60,80,100]"
          :total="listData.count"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog
        :close-on-click-modal="false"
        :title="editDialog.id? '编辑':'新增'"
        :visible.sync="editDialog.show"
        destroy-on-close
        top="80px"
        width="800px"
        :modal="false">
      <edit
          :businessId="id"
          v-if="editDialog.show"
          :id="editDialog.id"
          ref="editForm"
          @hideEditDialog="hideEditDialog"
      />
    </el-dialog>
    <el-dialog
        :close-on-click-modal="false"
        title="分类列表"
        :visible.sync="editClassificationDialog.show"
        destroy-on-close
        top="80px"
        width="1100px"
        :modal="false">
      <classification-list
          :businessId="id"
          v-if="editClassificationDialog.show"
          ref="editForm"
          @hideClassificationDialog="hideClassificationDialog"
      />
    </el-dialog>
  </div>
</template>

<script>
import edit from './edit.vue'
import {deleteRow, getActivityList, updateStatus} from "@/api/merchantActivity";
import {checkPermi} from "@/utils/permission";
import ClassificationList from "@/views/operate/business/merchantActivities/classification-list.vue";
export default {
  components: {ClassificationList, edit},
  props: {
    id: {
      default: null
    }
  },
  data() {
    return {
      listData: { list: [],count:0 },
      listPram: {
        page: 0,
        limit: 20,
        businessId:''
      },
      editDialog:{
        show:false,
        id:null,
      },
      editClassificationDialog:{
        show:false,
      },
      editDialogList:{
        show:false,
        id:null,
        editDate:{},
        pid:null
      },
      tableLoading:false,
      imageUrl: ''
    }
  },
  mounted() {
    this.imageUrl = process.env.VUE_APP_IMAGE_URL
    this.getList()
  },
  methods: {
    checkPermi,
    getList(){
      this.tableLoading=true
      this.listPram.businessId = this.id
      getActivityList(this.listPram).then(res=>{
        this.listData=res;
      }).finally(()=>{})
      this.tableLoading=false;
    },
    openEdit(id=null){
      this.editDialog.id=id;
      this.editDialog.show=true
    },
    openClassificationEdit(){
      this.editClassificationDialog.show=true
    },
    openEditList(id=null,editDate){
      this.editDialogList.id=id;
      this.editDialogList.show=true
      this.editDialogList.editDate=editDate
    },
    handlerDelete(id) {
      this.$confirm('确认删除该数据吗？').then(() => {
        const loading=this.$loading({text:'删除中',background:'rgba(255,255,255,.5)'})
        deleteRow(id).then(() => {
          this.$message.success('删除成功')
          this.getList()
        }).finally(()=>{
          loading.close()
        })
      })
    },
    hideEditDialog() {
      this.editDialog.show = false
      this.getList()
    },
    hideClassificationDialog() {
      this.editClassificationDialog.show = false
      this.getList()
    },
    editListDialog() {
      this.editDialogList.show = false
      this.getList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.getList()
    },
    handleStatusChange(status,id){
      const loading=this.$loading({text:'更新中',background:'rgba(255,255,255,.5)'})
      updateStatus(id,status).then(()=>{
        this.$message.success('更新状态成功')
        this.getList()
      }).finally(()=>{
        loading.close()
      })
    },
    resetQuery(){
      this.listPram.isReceive = '';
      this.getList()
    }
  }
}
</script>

<style scoped lang="scss">

</style>

<style scoped>

</style>
